<?php

namespace app\api\controller;

use app\common\model\Fight as FightModel;
use app\common\model\Game as GameModel;
use app\common\model\GameJoin;
use think\Exception;

class Fight extends Base
{



    public function fight_edit()
    {
        if (request()->isPost()) {
            $data = input('');
            if($data['gid']){
                $game = GameModel::getOne(['id'=>$data['gid']]);
                if(!$game){
                    return returnJson(202,'赛事不存在');
                }
            }

            if (!$data['name']) {
                return returnJson(202, '请输入球队名称');
            }
            if (!$data['age']) {
                return returnJson(202, '请输入队员年龄');
            }
            if (!$data['ball_age']) {
                return returnJson(202, '请输入球龄');
            }
            if (!$data['score']) {
                return returnJson(202, '请输入战绩');
            }
            if (!$data['member_name']) {
                return returnJson(202, '请输入球队队员');
            }
            if (!$data['color']) {
                return returnJson(202, '请输入队服颜色');
            }
            if (!$data['contact']) {
                return returnJson(202, '请输入球队负责人');
            }
            if (!$data['phone']) {
                return returnJson(202, '请输入球队负责人联系方式');
            }
            $save_data = [
                'name' => $data['name'],
                'uid' => $this->uid,
                'age' => $data['age'],
                'contact' => $data['contact'],
                'phone' => $data['phone'],
                'ball_age' => $data['ball_age'],
                'color' => $data['color'],
                'score' => $data['score'],
                'member_name' => $data['member_name'],
                'update_time' => time(),
                'create_time' => time(),
            ];
            $res = FightModel::create($save_data);
            if ($res) {
                if($data['gid']){
                    $fight_data = [
                        'gid' =>$data['gid'],
                        'fid' =>$res,
                        'update_time' =>time(),
                        'create_time' =>time(),
                        'uid' => $this->uid
                    ];
                    GameJoin::create($fight_data);
                }
                $content = $data['gid']?'球队创建成功，已成功加入比赛':'球队创建成功,请继续登记比赛信息';
                return returnJson(200, $content,$res);
            } else {
                return returnJson(202, '球队创建失败');
            }
        }
    }

}
